<?php
namespace AuctionCore\Dispute;

use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\Dispute\PurchaseOrder;
use AuctionCore\CarResource\CarModel;


class Dispute
{
    const STATUS_HANDLE = 0; // 待审核
    const STATUS_ADOPT = 1; // 已通过
    const STATUS_REJECT = 2; // 驳回

    const CUSTOMER_DEALER = 1; //车商
    const CUSTOMER_OWNER = 2; //车主

    protected $did = null;

    public function __construct($did)
    {
        $this->did = $did;
    }
    //添加争议
    public function DisputeAdd($data){
        $res_dispute = M('dispute')->add($data);
        //拍单锁定
        $o = new AuOrder($data['order_id']);
        $res_order = $o->lockByOrderId();
        ///采购单锁定
        $po = new PurchaseOrder($data['po_id']);
        $res_purchase_order = $po->lockById();

        //付款单锁定(采购单)
        $payment_id = $po->getPaymentIdByPoid();
        $res_payment = Payment::lock($payment_id);
        //收款锁定(拍单)
        $collection_id = $o->getCollectionIdByOid();
        $res_collection = $this->Collection_lock($collection_id);

        if ($data['od_type'] == self::CUSTOMER_DEALER)
            $customer = "dealer_id";
        elseif ($data['od_type'] == self::CUSTOMER_OWNER)
            $customer = "owner_id";
        if ($res_dispute && $res_order && $res_purchase_order && $res_payment && $res_collection) {
            //事务提交
            M()->commit();
            //记录操作日志
            $log = array('order_id' => $data['order_id'], $customer => $data['od_id'], 'action' => '产生争议');
            \AuctionCore\System\SystemLog::add($log);
            return true;
        } else {
            M()->rollback();
            \Think\Log::record('添加争议时发生回滚事件！order_id:' . $data['order_id'] . ',' . $customer . ':' . $data['od_id']);
            return false;
        }
    }
    public function get_modify_list($dealer_id,$page,$pagesize){
        $map['success_dealer_id'] = $dealer_id;
        $list = M("modify_list")->alias('ml')
            ->join(C('DB_PREFIX') . "breach_car as bc ON ml.`mid` = bc.`mid`")
            ->join(C('DB_PREFIX') . "order as o ON ml.`order_id` = o.`order_id`")
            ->where($map)->limit($pagesize)->page($page)->select();
        $total = M("modify_list")->alias('ml')
            ->join(C('DB_PREFIX') . "breach_car as bc ON ml.`mid` = bc.`mid`")
            ->join(C('DB_PREFIX') . "order as o ON ml.`order_id` = o.`order_id`")
            ->where($map)->count();
        if (!empty($list)) {
            $cm = new CarModel();
            $a = new \AuctionCore\Other\Address();
            foreach ($list as $key => $val) {
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $city_name = $a->get_name_by_code($val['location_area']);
                $list[$key]['model'] = '[' . $city_name . '] ' . $series['name'] . ' ' . $val['car_cc'] . ($val['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $val['gearbox']);
            }
        }
        return $result =  array(
            'list'=>$list,
            'total'=>$total
        );
    }
    //获取异常车辆详情
    public function get_modify_detail(){

    }
    public function get_dispute_list($dealer_id,$page,$pagesize){
        $map['success_dealer_id'] = $dealer_id;
        $list = M("dispute")->alias('d')
            ->join(C('DB_PREFIX') . "breach_car as bc ON d.`did` = bc.`did`")
            ->join(C('DB_PREFIX') . "order as o ON d.`order_id` = o.`order_id`")
            ->where($map)->limit($pagesize)->page($page)->select();
        $total = M("dispute")->alias('d')
            ->join(C('DB_PREFIX') . "breach_car as bc ON d.`did` = bc.`did`")
            ->join(C('DB_PREFIX') . "order as o ON d.`order_id` = o.`order_id`")
            ->where($map)->count();
        if (!empty($list)) {
            $cm = new CarModel();
            $a = new \AuctionCore\Other\Address();
            foreach ($list as $key => $val) {
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $city_name = $a->get_name_by_code($val['location_area']);
                $list[$key]['model'] = '[' . $city_name . '] ' . $series['name'] . ' ' . $val['car_cc'] . ($val['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $val['gearbox']);
            }
        }
        return $result =  array(
            'list'=>$list,
            'total'=>$total
        );
    }
    //获取异常车辆详情
    public function get_dispute_detail(){

    }
    /**
     * 锁定收款单
     * @param $id
     * @return bool
     */
    function Collection_lock($collection_id){
        if (intval($collection_id) < 1) return false;
        return M("collection")->where(['id' => $collection_id])->save(['status' => 2]);
    }
}